package com.example.xdzx.service;

import com.aliyun.oss.OSS;
import com.example.xdzx.mapper.ItemsMapper;
import com.example.xdzx.pojo.LostItems;
import com.example.xdzx.pojo.User;
import com.example.xdzx.utils.ItemsComparator;
import com.example.xdzx.utils.InitializeOssClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class ItemsService {
    @Autowired
    ItemsMapper itemsMapper;
    @Autowired
    UserService userService;

    /**
     * 获取所有失物信息
     * @return
     */
    public List<LostItems> getAllLost(){
        List<LostItems> list = itemsMapper.getAll();
        list.sort(new ItemsComparator());
        return list;
    }

    /**
     * 获取当前用户发布的失物列表
     * @param userId
     * @return
     */
    public List<Map<String,Object>> getMyRelease(Long userId){
        List<LostItems> list = itemsMapper.getReleaseById(userId);
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> res = new LinkedList<>();
        for(LostItems i : list){
            map.put("lostId",i.getLostId());
            map.put("userPic",i.getUserPic());
            map.put("userId",i.getUserId());
            map.put("userName",i.getUserName());
            map.put("createTime",i.getCreateTime());
            map.put("imageUrl",i.getImageUrl());
            map.put("capsuleAddr",i.getCapsuleAddr());
            map.put("capsuleGoods",i.getCapsuleGoods());
            res.add(map);
            map = new HashMap<String,Object>();
        }
        return res;
    }

    /**
     * 添加失物信息，图片上传oss服务器提供外链给前端进行显示
     * @param userId
     * @param capsuleGoods
     * @param capsuleAddr
     * @param type
     * @param file
     * @return
     */
    public Boolean release(Long userId, String capsuleGoods, String capsuleAddr, String type,
                           MultipartFile file){
       /* String myBucket = "xdzx-picture";
        OSS ossClient = InitializeOssClient.getMyOss();

        try{
            //文件上传oss服务器
            InputStream inputStream = file.getInputStream();
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            String fileName = uuid+suffix;
            ossClient.putObject(myBucket,"img/"+fileName,inputStream);
            String imageUrl = "https://xdzx-picture.oss-cn-chengdu.aliyuncs.com/img/"+fileName;
            try{
                itemsMapper.addRelease(userId,capsuleGoods,capsuleAddr,type,imageUrl);
            }catch (Exception e){
                return false;
            }
        }catch (IOException e){
            return false;
        }*/
        return true;
    }

    public Map<String,Object> uploadImg(MultipartFile file) {
        Map<String,Object> returnMap=new HashMap<>();
        String myBucket = "xdzx-picture";
        OSS ossClient = InitializeOssClient.getMyOss();
        try{
            //文件上传oss服务器
            InputStream inputStream = file.getInputStream();
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            String fileName = uuid+suffix;
            ossClient.putObject(myBucket,"img/"+fileName,inputStream);
            String imageUrl = "https://xdzx-picture.oss-cn-chengdu.aliyuncs.com/img/"+fileName;
            returnMap.put("imageUrl",imageUrl);
            returnMap.put("msg","上传成功！");
        }catch (IOException e){
            returnMap.put("isSuccess",false);
            returnMap.put("msg","上传失败！");
            return returnMap;
        }
        return returnMap;
    }

    public Boolean release(Long userId, String capsuleGoods, String capsuleAddr, String type, String userPic, String imageUrl) {
        try {
            // 根据userId 查询用户信息
            User user= userService.getUserById(userId);
            itemsMapper.addRelease(userId,user.getUsername(),capsuleGoods,capsuleAddr,type,imageUrl,userPic);
            return Boolean.TRUE;
        } catch (Exception e) {
            return Boolean.FALSE;
        }
    }

    public List<LostItems> getLostList(LostItems lostItems) {
        List<LostItems> list = itemsMapper.getLostList(lostItems);
        list.sort(new ItemsComparator());
        return list;
    }
}
